package hlhj.fhp.newslib.adapters

import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.util.Log
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.activitys.SpecialInfoAty
import hlhj.fhp.newslib.javabean.SpecialDetailBean
import hlhj.fhp.newslib.network.Urls
import hlhj.fhp.newslib.utils.GlideUtil
import hlhj.fhp.newslib.utils.NumUtil
import java.util.ArrayList

/**
 * Created by Never Fear   on 2018\10\12 0012.
Never More....
 */
class SpecialSingleAdp(private val datas: ArrayList<SpecialDetailBean.DataBean.ListBean>)
    :BaseQuickAdapter<SpecialDetailBean.DataBean.ListBean,BaseViewHolder>(R.layout.special_news_detail_item) {
    override fun convert(helper: BaseViewHolder?, item: SpecialDetailBean.DataBean.ListBean?) {
        if (item?.type==1){
            if (item?.extract_img!!.size>0){
                if (item?.extract_img[0].contains("http")){
                    Glide.with(mContext).load(item?.extract_img[0]).apply(GlideUtil.loadIMGOption())
                            .into(helper?.getView(R.id.itemImg)!!)
                }else{
                    Glide.with(mContext).load(Urls.Base_URL+"/"+item?.extract_img[0]).apply(GlideUtil.loadIMGOption())
                            .into(helper?.getView(R.id.itemImg)!!)
                }
            }
            helper?.setText(R.id.itemTittle,item?.title)
//                helper?.setText(R.id.itemFrom,item?.release)
            if (item?.click_rate>=1000){
                helper?.setText(R.id.itemViewNum,"${NumUtil.formatNum((item?.click_rate/1000f),1)}")
            }else{

                helper?.setText(R.id.itemViewNum,"${item?.click_rate}")
            }
            helper?.setTextColor(R.id.itemTime,Color.parseColor("#777777"))
            helper?.setText(R.id.itemTime,item?.add_time)
            helper?.itemView!!.setOnClickListener {
                data[helper?.adapterPosition].click_rate=data[helper?.adapterPosition].click_rate+1
                notifyDataSetChanged()
                val intent = Intent(mContext, SpecialInfoAty::class.java)
                if (item?.extract_img.size>0){
                    intent.putExtra("thumb",item?.extract_img[0])
                }else{
                    intent.putExtra("thumb","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531470814752&di=49e3232f35fa9c8439160a2d071bf1cb&imgtype=0&src=http%3A%2F%2Fimg382.ph.126.net%2Fp4dMCiiHoUGxf2N0VLspkg%3D%3D%2F37436171903673954.jpg")
                }
                intent.putExtra("id",item?.id)
                mContext.startActivity(intent)
            }
        }else{
            helper?.setText(R.id.itemTittle,item?.title)
            helper?.setText(R.id.itemTime,"广告")
            helper?.setTextColor(R.id.itemTime,Color.parseColor("#FFB400"))
            if (item!!.advertisement_img[0]!=null){
                if (item!!.advertisement_img[0].contains("http")){
                    Log.e("fhp","有http")
                    Glide.with(mContext).load(item?.advertisement_img?.get(0))
                            .apply(GlideUtil.loadIMGOption())
                            .into(helper?.getView(R.id.itemImg)!!)
                }else{
                    Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(0))
                            .apply(GlideUtil.loadIMGOption())
                            .into(helper?.getView(R.id.itemImg)!!)
                }
            }
            helper?.itemView!!.setOnClickListener {
                val uri = Uri.parse(item?.advertisement_url)
                val intent = Intent(Intent.ACTION_VIEW, uri)
                mContext.startActivity(intent)
            }
        }

    }
}